﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_MyUserControls_TH_ChiTietTinTuc : System.Web.UI.UserControl
{
    private DataSet objDataSet;
    private int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        id = int.Parse(Request.QueryString["id"].ToString());

        if (!IsPostBack)
        {
            // Fill controls with data
            LoadChiTietTinTuc();
        }
    }

    public void LoadChiTietTinTuc()
    {
        TinTucBUS objTinTuc = new TinTucBUS(TGDDConfiguration.DbConnectionString);
        objDataSet = objTinTuc.SelectTinTuc(id);
        DataRow row = objDataSet.Tables[0].Rows[0];
        txtTieuDe.Text = row["TieuDe"].ToString();
        txtNoiDung.Content = row["NoiDung"].ToString();
        lblNgayDang.Text = row["NgayDang"].ToString();
        lblNguoiDang.Text = row["Ten"].ToString();
        imgHinhMinhHoa.ImageUrl = Request.ApplicationPath + "/images/tintuc/" +
                row["HinhMinhHoa"].ToString();
    }

    private void MessageBox(string msg)
    {
        Page.Controls.Add(new LiteralControl(
         "<script language='javascript'> window.alert('" + msg.Replace("'", "\\'") + "');</script>"));
    }

    protected void lkbtnCapNhat_Click(object sender, EventArgs e)
    {
        TinTucBUS objTinTuc = new TinTucBUS(TGDDConfiguration.DbConnectionString);
        TinTucDTO ttDto = new TinTucDTO();
        ttDto.Id = id;
        ttDto.TieuDe = txtTieuDe.Text;
        ttDto.NoiDung = txtNoiDung.Content;
        ttDto.HinhMinhHoa = fileUploadHinhAnh.FileName;

        int type = 0;
        if (!fileUploadHinhAnh.FileName.Equals(""))
            type = 1;
        if (objTinTuc.UpdateTinTuc(ttDto, type))
        {
            if (type == 1)
            {
                string strBaseLocation = Server.MapPath("..") + "\\images\\tintuc\\" + fileUploadHinhAnh.FileName;

                if (fileUploadHinhAnh.PostedFile != null)
                {
                    fileUploadHinhAnh.PostedFile.SaveAs(strBaseLocation);
                }
            }
            MessageBox("Cập nhật thành công");
        }
        else
        {
            MessageBox("Cập nhật thất bại");
        }
    }
}
